package com.itchen.hospital.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itchen.hospital.Msg.DefaultInfo;
import com.itchen.hospital.exception.CustomException;
import com.itchen.hospital.mapper.HospitalMapper;
import com.itchen.hospital.pojo.Admin;
import com.itchen.hospital.pojo.Params;
import com.itchen.hospital.pojo.Hospital;
import com.itchen.hospital.service.HospitalService;
import com.itchen.hospital.service.HospitalService;
import com.itchen.hospital.utils.JwtTokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class HospitalServiceImpl implements HospitalService {

    private final HospitalMapper hospitalMapper;

    //条件查询
    @Override
    public PageInfo<Hospital> search(Params params) {
        //处理分页
        PageHelper.startPage(params.getPageNum(), params.getPageSize());

        if (JwtTokenUtils.getCurrentUser().getRole().equals(DefaultInfo.DefaultUserName)){
            params.setPatientName(JwtTokenUtils.getCurrentUser().getUsername());
        }

        List<Hospital> list = hospitalMapper.findBySearch(params);
        return PageInfo.of(list);
    }


    //修改
    @Override
    public void update(Map<String, String> map) {
        Hospital hospital = new Hospital();
        hospital.setId(Integer.valueOf(map.get("id")));
        hospital.setPatientName(map.get("patientName"));
        hospital.setRoomNumber(map.get("roomNumber"));
        hospital.setRoomPrice(map.get("roomPrice"));
        hospital.setPayFeeState(map.get("payFeeState"));
        hospital.setHospitalInsurance(map.get("hospitalInsurance"));
        hospital.setRoomState(map.get("roomState"));
        hospitalMapper.update(hospital);
    }

    //根据id查询
    @Override
    public Hospital findById(Integer id) {
        Hospital patient = hospitalMapper.findById(id);
        return patient;
    }

}
